package head.first.model.state.demo2;

import apple.laf.JRSUIConstants;

/**
 * Created by yiyongyao on 2018/2/27.
 *
 * 糖果机器  context
 *
 */
public class GumballMachine {

    State soldOutState;
    State noQuarterState;
    State hasQuarterState;
    State soldState;
    //初始化 糖果机器为已售罄状态
    State state = soldOutState;
    int count=0;


    //初始化给糖果机装tang糖
    public GumballMachine(int numberGumballs)
    {
        soldOutState= new SoldOutState(this);
        noQuarterState = new noQuarterState(this);
        hasQuarterState = new hasQuarterState(this);
        soldState = new SoldState(this);

        this.count = numberGumballs;
        //装糖后修改状态为未投币
        if(numberGumballs>0)
        {
            state = noQuarterState;
        }
    }


    public void setState(State state) {
        this.state = state;
    }

    public void insertQuarter()
    {
        state.insertQuarter(this);
    }

    public void ejectQuarter()
    {
        state.ejectQuarter(this);
    }


    public void turnCrank()
    {
        state.turnCrank(this);
    }


    public void dispence()
    {
        state.dispence(this);
    }


    public State getSoldOutState() {
        return soldOutState;
    }

    public State getNoQuarterState() {
        return noQuarterState;
    }

    public State getHasQuarterState() {
        return hasQuarterState;
    }

    public State getSoldState() {
        return soldState;
    }

    public int getCount() {
        return count;
    }

    public void releaseBall() {
        System.out.println("正在分发糖果.....还有"+count+"个糖果");
        if(count!=0)
        {
            count--;
        }
    }
}
